package com.zj.config;

import com.zj.service.OrderToolService;
import com.zj.service.ProductToolService;
import com.zj.service.ToolService;
import dev.langchain4j.community.model.dashscope.QwenEmbeddingModel;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.StreamingChatModel;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever;
import dev.langchain4j.service.*;

import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;
import dev.langchain4j.store.memory.chat.ChatMemoryStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AIConfig {
    //操作的接口中
    public interface AiAssistant {
        @SystemMessage("你是一个电商网站的智能助手")
        String  chat(@MemoryId String memoryId, @UserMessage String message);

        @SystemMessage("你是一个电商网站的智能助手")
        TokenStream chatStream(@MemoryId String memoryId,@UserMessage String message);
    }

    //生成上面接口的实现类，并托管到spring容器
    @Bean
    public AiAssistant aiService(ChatModel chatModel,
                                 StreamingChatModel streamingChatModel,
                                 ChatMemoryStore chatMemoryStore,

                                 ToolService toolService, //注入工具类
                                 ProductToolService productToolService,
                                 OrderToolService orderToolService,

                                 EmbeddingStore embeddingStore,
                                 QwenEmbeddingModel qwenEmbeddingModel
    ){
        //TODO：改进方案基于token的记忆窗口
        //ChatMemoryProvider 用于创建和管理聊天内存
        ChatMemoryProvider chatMemoryProvider = memoryId-> MessageWindowChatMemory.builder()
                .id(memoryId)
                .maxMessages(1000)
                .chatMemoryStore(chatMemoryStore)//记忆存储
                .build();


        //相较于基础的AI工具，多加一个内容检索器，检索器的作用是根据查询的关键词，从向量数据库中检索出最相关的文本片段
        ContentRetriever contentRetriever= EmbeddingStoreContentRetriever.builder()
                .embeddingStore(embeddingStore)
                .embeddingModel(qwenEmbeddingModel)
                .maxResults(1)  //最多返回2条结果
                .minScore(0.5)  //
                .build();

        AiAssistant aiAssistant = AiServices.builder(AiAssistant.class)
                .chatModel(chatModel)
                .streamingChatModel(streamingChatModel)
                .chatMemoryProvider(chatMemoryProvider) //存储记忆
                .tools(toolService,productToolService,orderToolService)//注入工具类  增强工具类

                //TODO:MCP服务  列表  -->增强功能
                //TODO: RAG  -->增强知识 库
                .contentRetriever(contentRetriever)
                .build();
        return aiAssistant;
    }

    @Bean
    public EmbeddingStore embeddingStore(){
        return new InMemoryEmbeddingStore();
    }
}
